package server.version;

import servlet.HttpProtocolUtil;

import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by louisyuu on 2020/4/2 上午11:03
 * <p>
 * <p>
 * 完成Minicat 1.0版本
 * 需求：浏览器请求http://localhost:8080,返回一个固定的字符串到页面"Hello Minicat!"
 */
public class VersionOneStarter implements VersionStater {


    @Override
    public void start() throws Exception {

        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("=====>>>Minicat start on port：" + port);

        while (true) {
            Socket socket = serverSocket.accept();
            // 有了socket，接收到请求，获取输出流
            OutputStream outputStream = socket.getOutputStream();
            String data = "Hello Minicat!";
            /**
             * 必须要设置Http头  因为是Http协议
             */
            String responseText = HttpProtocolUtil.getHttpHeader200(data.getBytes().length) + data;
            outputStream.write(responseText.getBytes());
            socket.close();
        }
    }
}
